﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Wolfpack.Periscope.Core.Interfaces;
using Wolfpack.Periscope.Core.Interfaces.Entities;
using Wolfpack.Periscope.Core.Repositories;
using Wolfpack.Periscope.Core.Widgets;
using File = System.IO.File;

namespace Wolfpack.Periscope.Tests
{
    public class TestConstants
    {
        public static string ConfigFilename = @"config\dashboard.config";
    }

    [TestFixture]
    public class WhenLoadingConfigurationIfNotExists
    {
        private IDashboardConfigurationRepository _sut;
        private DashboardConfiguration _config;

        [SetUp]
        public void Given()
        {
            _sut = new FileSystemDashboardRepository();
            if (File.Exists(TestConstants.ConfigFilename))
                File.Delete(TestConstants.ConfigFilename);

            _config = _sut.Load();
        }

        [Test]
        public void ThenItShouldEstablishTheConfigFile()
        {
            Assert.That(File.Exists(TestConstants.ConfigFilename));
        }
    }

    [TestFixture]
    public class WhenSavingConfiguration
    {
        private IDashboardConfigurationRepository _sut;
        private DashboardConfiguration _expectedConfig;

        [SetUp]
        public void Given()
        {
            _sut = new FileSystemDashboardRepository();
            var panels = new List<DashboardPanelConfiguration>
            {
                new DashboardPanelConfiguration
                {
                    Id = new Guid("5D4E8CD1-7FD5-4159-816A-6E602AD59AA4"),
                    DwellInSeconds = 123,
                    Name = "Panel1",
                    Sequence = 1,
                    Widgets = new List<WidgetInstance>
                    {
                        new WidgetInstance
                        {
                                Column = 1,
                                Row = 1,
                                CssTemplate = "Widget1_Css",
                                Description = "Widget1_desc",
                                Height = 2,
                                MarkupTemplate = "Widget1_Markup",
                                Id = "Widget1",
                                SampleTemplate = "Widget1_Sample",
                                ScriptIncludes = new List<Property>
                                {
                                    new Property{ Name = "Script1", Value = "http://script1"},
                                    new Property{ Name = "Script2", Value = "http://script2"}
                                },
                                ScriptTemplate = "Widget1_Script",
                                StyleSheetIncludes = new List<Property>
                                {
                                    new Property{ Name = "Css1", Value = "http://css1"},
                                    new Property{ Name = "Css2", Value = "http://css2"}
                                },
                                TemplateData = new Dictionary<string, string>
                                {
                                    { "custom_data1", "custom_value1" },
                                    { "custom_data2", "custom_value2" }
                                },
                                TemplatesFolder = "CustomTemplateFolder",
                                Title = "Widget1_Title",
                                Width = 2,

                                RendererId = "TestRenderer",
                        }
                    }
                },
            };
            _expectedConfig = new DashboardConfiguration
            {
                Panels = panels
            };

            _sut.Save(_expectedConfig);
        }

        [Test]
        public void ThenTheConfigurationChangesShouldBePersisted()
        {
            var actualConfig = _sut.Load();
            Assert.That(actualConfig.Panels.Count(), Is.EqualTo(2));
        }
    }
}